package controller;

import pojo.User;
import service.UserService;
import view.MainJFrame;
import view.Register;
import view.SignIn;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegisterListener implements ActionListener {
    private SignIn signIn;
    private Register register;
    private MainJFrame frame;
    public RegisterListener(Register dialog) {
        super();
        this.register = dialog;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("1")) {
            if (register.getTfNum().getText().trim().equals("")||register.getTfPwd().getText().trim().equals("")||register.getTfGender().getText().trim().equals("")||register.getTfPN().getText().trim().equals("")||register.getTfName().getText().trim().equals("")) {
                JOptionPane.showMessageDialog(register, "注册失败,请重新输入完整信息", "提示", JOptionPane.WARNING_MESSAGE);}
            else{
                String userId = register.getTfNum().getText();
                String userName = register.getTfName().getText();
                String passWord = register.getTfPwd().getText();
                String gender = register.getTfGender().getText();
                String phoneNumber = register.getTfPN().getText();
                String type = "普通用户";
                User user = new User(userId, userName, passWord, gender, phoneNumber, type);
                UserService userService = new UserService();
                userService.add(user);
                int a = JOptionPane.showConfirmDialog(register, "确认注册?", "确认信息", JOptionPane.YES_NO_OPTION);
                if (a == JOptionPane.YES_OPTION) {
                    register.dispose();

                }
            }
        }
        if (e.getActionCommand().equals("2")) {
            register.dispose();
        }
    }
}
